//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 			VisualNovelToolkit		     /_/_/_/_/_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe.	/_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;

[ CustomEditor( typeof( ScenarioNode ) ) ] 
public class ScenarioNodeInspector : Editor {
	private string scenarioScript;

	static private void CompileAndSaveAsBinaryFile( string fileName , ScenarioNode scenario ){
		ByteCodes btcodes = scenario.Compile();
		ViNode.SaveByteCodesToFile( fileName , btcodes.GetCode() );					
	}

	public override void OnInspectorGUI(){		
		ScenarioNode scenario = target as ScenarioNode;
		
		Color savedCol = GUI.backgroundColor;
		GUI.backgroundColor = Color.green;
		
		GUILayout.BeginHorizontal();
					
			scenario.m_PlayAtStart = EditorGUILayout.Toggle( "Play at Start" , scenario.m_PlayAtStart );//, GUILayout.Width( 15f ) );		
					
		GUILayout.EndHorizontal();

		GUI.enabled = false;
		GUILayout.BeginHorizontal();
			scenario.startFromSave = EditorGUILayout.Toggle( "Start from SavePoint" , scenario.startFromSave );//, GUILayout.Width( 15f ) );		
						
		GUILayout.EndHorizontal();
			GUI.enabled = true;
						
		scenario.flagTable = EditorGUILayout.ObjectField( "Flag" , scenario.flagTable , typeof( FlagTable ) , false ) as FlagTable;

		GUILayout.BeginHorizontal();
		
			scenario.useScenarioScript = EditorGUILayout.Toggle( "use Scenario Script" , scenario.useScenarioScript );// , GUILayout.Width( 15f ) );
//				EditorGUILayout.LabelField( "useScenarioScript" , GUILayout.Width( 100f ) );

		GUILayout.EndHorizontal();

		if( scenario.useScenarioScript ){
			scenario.scenarioScript = EditorGUILayout.ObjectField( "TextAsset" , scenario.scenarioScript , typeof( TextAsset ) , false  ) as TextAsset;
		}
		else{
//		GUI.enabled = ! scenario.useScenarioScript;

			GUILayout.BeginHorizontal();
				scenario.startNode = (ViNode) EditorGUILayout.ObjectField( "START NODE" , scenario.startNode , typeof( ViNode) , true );
			GUILayout.EndHorizontal();

		}

//		GUI.enabled = true;
		
//		GUI.enabled = Application.isPlaying;
//		NodeGUI.OnGUI_ViNode( scenario , true , false );

		if( Application.isPlaying ){

			GUILayout.BeginHorizontal();

				GUILayout.Space( 30f );

			if( GUILayout.Button( "PLAY" ) ){
				if( AssetDatabase.Contains( scenario.gameObject )  ){
					GameObject clone = Instantiate( scenario.gameObject ) as GameObject;
					ViNoGOExtensions.StripGameObjectName( clone , "(Clone)" , "" );
				}
				else{
					scenario.Play();
				}
			}			
			if( GUILayout.Button ( "Save as ScenarioScript" ) ){
				SaveAsScenarioScript( scenario );
			}			
				GUILayout.Space( 30f );
			
			GUILayout.EndHorizontal();

		}
		else{

			GUILayout.BeginHorizontal();

				GUILayout.Space( 30f );

			if( AssetDatabase.Contains( scenario.gameObject )  ){
				if( GUILayout.Button( "Edit" ) ){				
					EditorUtility.DisplayDialog( "Note" , "If you finish editing in your scene , please do not forget to apply changes to the prefab" , "Ok" );
					GameObject obj = PrefabUtility.InstantiatePrefab( scenario.gameObject ) as GameObject;//AttachedAsset( scenario.gameObject );
					PrefabUtility.ReplacePrefab( obj, scenario.gameObject, ReplacePrefabOptions.ConnectToPrefab);					

					EditorGUIUtility.PingObject( obj );
				}
			}
			else{
				if( GUILayout.Button ( "Save as ScenarioScript" ) ){
					SaveAsScenarioScript( scenario );
				}			
			}

				GUILayout.Space( 30f );
			
			GUILayout.EndHorizontal();
		}
										
		GUI.backgroundColor = savedCol;
//		serializedObject.ApplyModifiedProperties();
	}

	/// <summary>
		/// Saves as scenario script file.
	/// </summary>
	/// <param name="scenario">Scenario.</param>
	void SaveAsScenarioScript( ScenarioNode scenario ){
		string text = ViNoToolkit.KAGInterpreter.ToScenarioScript( scenario.gameObject );
		string filePath = Application.dataPath + "/" + scenario.name + ".txt";
		Debug.Log( "Saved as a Scenario Script at :" + filePath );
		File.WriteAllText( filePath , text );
		AssetDatabase.Refresh();
	}
	
}
